﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Web.Services;
using System.Text;
using System.Text.RegularExpressions;

namespace Catering.Catering_Page.Caipin.Ccaipin_jcsz
{
    public partial class Caipinshezhi : UserIsNull
    {
        public DataTable dt_RawMaterials = new DataTable();
        public DataTable dt_RawMaterialsInfo = new DataTable(); //原料分类
        public DataTable dt_RawMaterial = new DataTable();

        public DataTable dt_DishType = new DataTable();
        public DataTable dt_ChildDishType = new DataTable();
        public DataTable dt_Dish = new DataTable();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.LoadData();
            }
        }

        //保定页面数据
        public void LoadData()
        {
            this.dt_RawMaterials = new Catering_Rules.T_RawMaterials().GetAllList().Tables[0]; //原料大类
            this.dt_RawMaterialsInfo = new Catering_Rules.RawMaterialsInfo().GetAllList().Tables[0];//原料细类
            this.LabDishCount.Text = new Catering_Rules.T_Dish().GetCount("").ToString();
        }

        /// <summary>
        /// 获取菜品树形Html
        /// </summary>
        /// <returns>返回拼接完成的树形Html</returns>
        public string GetDishList()
        {
            dt_DishType = new Catering_Rules.DishType().GetAllList(false).Tables[0];
            dt_Dish = new Catering_Rules.T_Dish().GetList("").Tables[0];//获取本区域下的所有菜品AreaIDS like '%," + LoginPersonnel.AreaID + ",%'
            string TypeTemp = "<li><a iscai='false' IsType='true' IsTypeInfo='false' TypeID='{$id}'>{$DT}</a>#content#</li>";
            string DishTemp = "<li><a iscai='true' uid='{$id}'>{$Name}</a></li>";
            StringBuilder sbdish = new StringBuilder();
            DataRow[] TypeList = dt_DishType.Select("ParentID=0");
            string DishHtml = this.AddDishHtml(TypeList, TypeTemp, DishTemp);
            sbdish.Append(DishHtml);
            return sbdish.ToString();
        }

        /// <summary>
        /// 获取菜品树形菜单
        /// </summary>
        /// <param name="TypeList">类别集合</param>
        /// <param name="TypeTemp">类别循环模板</param>
        /// <param name="DishTemp">菜品循环模板</param>
        /// <returns>返回菜品树形菜单</returns>
        private string AddDishHtml(DataRow[] TypeList, string TypeTemp, string DishTemp)
        {
            StringBuilder TypeHtml = new StringBuilder();
            foreach (DataRow row in TypeList)
            {
                string Temp = this.GetLoopContent(row, TypeTemp);
                DataRow[] child = dt_DishType.Select("ParentID=" + row["id"].ToString());
                string TempTypeHtml = this.AddDishHtml(child, TypeTemp, DishTemp);
                string TempDishHtml = this.GetDishHtml(row["id"].ToString(), DishTemp);
                string Tempcontents=string.Format("<ul class='treeview'>{0}{1}</ul>", TempTypeHtml, TempDishHtml);
                TypeHtml.Append(Temp.Replace("#content#", Tempcontents));
            }
            return TypeHtml.ToString();
        }

        /// <summary>
        /// 根据类别ID 获取对应的菜品Html集合
        /// </summary>
        /// <param name="DTID">菜品类别ID</param>
        /// <returns>返回所有菜品的Html</returns>
        private string GetDishHtml(string DTID, string DishTemp)
        {
            StringBuilder DishHtml = new StringBuilder();
            foreach (DataRow row in dt_Dish.Select("DTID="+DTID)) //遍历该类别下的所有菜品
            {
                string Temp = this.GetLoopContent(row, DishTemp);
                DishHtml.Append(Temp);
            }
            return DishHtml.ToString();
        }

        /// <summary>
        /// 获取已经处理过的LoopContent标签内容
        /// </summary>
        /// <param name="dt">数据源(DataTable)</param>
        /// <param name="LoopTempate">模版内容</param>
        /// <returns></returns>
        public string GetLoopContent(DataRow row, string LoopTempate)
        {
            LoopTempate = LoopTempate.Replace("{$", "{");
            StringBuilder LoopContent = new StringBuilder();
            Regex Re = new Regex(@"{(.[^{}]+)}");
            MatchCollection Mats = Re.Matches(LoopTempate);
            StringBuilder LoopItem = new StringBuilder(LoopTempate);
            foreach (Match Mat in Mats)
            {
                LoopItem.Replace(Mat.Value, row[Re.Replace(Mat.Value, "$1")].ToString());
            }
            LoopContent.Append(LoopItem.ToString());
            LoopItem = null; Re = null; Mats = null;
            return LoopContent.ToString();
        }

        //删除菜
        protected void btn_Del_Dish_Click(object sender, EventArgs e)
        {
            string DishID = this.HidID.Value;
            DataTable dt = new Catering_Rules.T_RecipeInformation().GetList("CID=" + DishID).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                int MID = Convert.ToInt32(dt.Rows[0]["MID"].ToString());
                Catering_Model.E_Recipes md_R = new Catering_Rules.T_Recipes().GetModel(MID);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('食谱：" + md_R.CateringName + " 中已经使用了该菜品，无法删除！');window.location='Caipinshezhi.aspx';</script>");
                return;
            }
            new Catering_Rules.DishNutrien().DeleteListBywhere("Dishid=" + DishID);
            new Catering_Rules.T_DishInfo().DeleteListBywhere("DID=" + DishID);
            new Catering_Rules.T_Dish().Delete(Convert.ToInt32(DishID));
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除成功！');window.location='Caipinshezhi.aspx';</script>");
        }

    }
}